package com.dhcc.DRGsHosp.facade.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.QuotaManageApplication;
import com.dhcc.DRGsHosp.core.domain.QuotaManage;
import com.dhcc.DRGsHosp.facade.QuotaManageFacade;
import com.dhcc.DRGsHosp.facade.dto.QuotaManageDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.QuotaManageAssembler;

@Named("drgsHospFacadeImpl39")
public class QuotaManageFacadeImpl implements QuotaManageFacade {

	@Autowired
	private QuotaManageApplication  application;

	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	public InvokeResult getQuotaManage(Double qmId) {
		return InvokeResult.success(QuotaManageAssembler.toDTO(application.getQuotaManage(qmId)));
	}
	
	public InvokeResult creatQuotaManage(QuotaManageDTO quotaManageDTO) {
		application.creatQuotaManage(QuotaManageAssembler.toEntity(quotaManageDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult updateQuotaManage(QuotaManageDTO quotaManageDTO) {
		//application.updateQuotaManage(QuotaManageAssembler.toEntity(quotaManageDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult removeQuotaManage(Double qmId) {
		application.removeQuotaManage(application.getQuotaManage(qmId));
		return InvokeResult.success();
	}
	
	public InvokeResult removeQuotaManages(Double[] qmIds) {
		Set<QuotaManage> quotaManages= new HashSet<QuotaManage>();
		for (Double qmId : qmIds) {
			quotaManages.add(application.getQuotaManage(qmId));
		}
		application.removeQuotaManages(quotaManages);
		return InvokeResult.success();
	}
	
	public List<QuotaManageDTO> findAllQuotaManage() {
		return QuotaManageAssembler.toDTOs(application.findAllQuotaManage());
	}
	
	public Page<QuotaManageDTO> pageQueryQuotaManage(QuotaManageDTO queryVo, int currentPage, int pageSize) {
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _quotaManage from QuotaManage _quotaManage   where 1=1 ");
	   	if (queryVo.getParquotaCode() != null && !"".equals(queryVo.getParquotaCode())) {
	   		jpql.append(" and _quotaManage.parquotaCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getParquotaCode()));
	   	}		
	   	if (queryVo.getParDepCode() != null && !"".equals(queryVo.getParDepCode())) {
	   		jpql.append(" and _quotaManage.parDepCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getParDepCode()));
	   	}		
	   	if (queryVo.getPardrgCode() != null && !"".equals(queryVo.getPardrgCode())) {
	   		jpql.append(" and _quotaManage.pardrgCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getPardrgCode()));
	   	}		
	   	if (queryVo.getManageYear() != null && !"".equals(queryVo.getManageYear())) {
	   		jpql.append(" and _quotaManage.manageYear like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getManageYear()));
	   	}		
	   	/*if (queryVo.getManageType() != null && !"".equals(queryVo.getManageType())) {
	   		jpql.append(" and _quotaManage.manageType like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getManageType()));
	   	}	*/	
	   	if (queryVo.getParDepGroupCode() != null && !"".equals(queryVo.getParDepGroupCode())) {
	   		jpql.append(" and _quotaManage.parDepGroupCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getParDepGroupCode()));
	   	}		
        Page<QuotaManage> pages = getQueryChannelService()
		   .createJpqlQuery(jpql.toString())
		   .setParameters(conditionVals)
		   .setPage(currentPage, pageSize)
		   .pagedList();
		   
        return new Page<QuotaManageDTO>(pages.getStart(), pages.getResultCount(),pageSize, QuotaManageAssembler.toDTOs(pages.getData()));
	}

	@Override
	public List<Map<String,Object>> findQuotaManageByparMsCode(String parMsCode) {
		List<Map<String,Object>> list = application.findQuotaManageByparMsCode(parMsCode) ;
		return list;
	}

	@Override
	public List<String> getOtherHspManageYears(String parMsCode) {
		List<String> list = application.getOtherHspManageYears(parMsCode) ;
		return list;
	}
	
	
}
